package com.paic.mhis.hcpms.workingArea.biz.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.paic.mhis.common.util.BusinessServiceUtil;
import com.paic.mhis.hcpms.warning.dto.PaginationQueryDTO;
import com.paic.mhis.hcpms.warning.dto.WarningGtasksDTO;
import com.paic.mhis.hcpms.warning.util.WarnConstants;
import com.paic.mhis.hcpms.workingArea.biz.service.WarnWorkingAreaService;
import com.paic.mhis.hcpms.workingArea.intergration.dao.WarnWorkingAreaDAO;
import com.paic.pafa.app.biz.service.BusinessServiceException;

/**
 * 
 * @author EX-chenyonghong
 * 
 */
@Service("warnWorkingAreaService")
public class WarnWorkingAreaServiceImpl implements WarnWorkingAreaService {

	@Autowired
	public WarnWorkingAreaDAO warnWorkingAreaDao;

	@Override
	public Map<String, Object> getGTasks(PaginationQueryDTO paginationCon) throws BusinessServiceException {

		// TODO Auto-generated method stub
		Map<String, Object> result = new HashMap<String, Object>();
		List<WarningGtasksDTO> taskList = new ArrayList<WarningGtasksDTO>();
		try {
			taskList = warnWorkingAreaDao.getGtasks(paginationCon);
			result.put(WarnConstants.SUCCESS_RESULT_KEY, taskList);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;

	}

	@Override
	public Map<String, Object> findWarningCount(Map<String, Object> condition) throws BusinessServiceException {
		// TODO Auto-generated method stub
		Map<String, Object> result = new HashMap<String, Object>();
		List<WarningGtasksDTO> taskList = new ArrayList<WarningGtasksDTO>();
		try {
			List<String> dateViewList = new ArrayList<String>();
			List<String> dateList = getDateList();
			condition.put("dateList", dateList);
			// 预警事件
			List<Map> resultNum = warnWorkingAreaDao.findWarningCount(condition);
			List<String> questionNum = new ArrayList<String>();
			for (int i = 0; i < dateList.size(); i++) {
				String dateTemp = dateList.get(i);
				String viewDate = dateTemp.substring(2, 4) + "-" + dateTemp.substring(4);
				for (int j = 0; j < resultNum.size(); j++) {
					Map tempMap = resultNum.get(j);
					String hearDate = (String) tempMap.get("hearDate");
					BigDecimal num = (BigDecimal) tempMap.get("num");
					if (dateTemp.equals(hearDate)) {
						questionNum.add(String.valueOf(num));
						break;
					}
					if (j == resultNum.size() - 1) {
						questionNum.add("0");
					}
				}
				dateViewList.add(viewDate);
			}
			result.put("warnNumList", questionNum);
			result.put("dateViewList", dateViewList);
			// 不合理费用
			condition.put("errorFee", "Y");
			questionNum = new ArrayList<String>();
			resultNum = warnWorkingAreaDao.findWarningCount(condition);
			for (int i = 0; i < dateList.size(); i++) {
				String dateTemp = dateList.get(i);
				for (int j = 0; j < resultNum.size(); j++) {
					Map tempMap = resultNum.get(j);
					String hearDate = (String) tempMap.get("hearDate");
					BigDecimal num = (BigDecimal) tempMap.get("num");
					if (dateTemp.equals(hearDate)) {
						questionNum.add(String.valueOf(num));
						break;
					}
					if (j == resultNum.size() - 1) {
						questionNum.add("0");
					}
				}
			}
			result.put("errorCostList", questionNum);
		} catch (Exception e) {
			BusinessServiceUtil.processException(e);
		}
		return result;
	}

	private List<String> getDateList() {
		Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.CHINESE);
		calendar.setTime(new Date());
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		List<String> dateList = new ArrayList<String>();
		String dateTemp = "";
		if (month < 10) {
			dateTemp = year + "0" + month;
		} else {
			dateTemp = year + "" + month;
		}

		dateList.add(dateTemp);
		for (int i = 1; i < 12; i++) {
			int tempMon = month - i;
			int tempYear = year;
			if (tempMon <= 0) {
				tempMon = tempMon + 12;
				tempYear = tempYear - 1;
			}
			if (tempMon < 10) {
				dateTemp = tempYear + "0" + tempMon;
			} else {
				dateTemp = tempYear + "" + tempMon;
			}

			dateList.add(dateTemp);
		}
		Collections.reverse(dateList);
		return dateList;
	}

}
